<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        td{
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <table border="1" width="400" height="400" style="border-collapse: collapse;">
            <thead>
                <th>全选<input  @change="checkall()" type="checkbox" v-model="allchecked"></th>
                <th>ID</th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品数量</th>
            </thead>
            <tbody>
                <tr v-for="item in goodsList" :key="item.id">
                    <td><input type="checkbox" v-model="item.checked"></td>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <button @click="item.num==0?'':item.num--">-</button>
                        {{item.num}}
                        <button @click='item.num++'>+</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <h2>{{totalPrice}}</h2>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                allchecked:false,
                goodsList: [
                    {
                        id: 1,
                        name: 'macbook',
                        price: 12999,
                        num: 1,
                        checked:true
                    },
                    {
                        id: 2,
                        name: '刘为',
                        price: 0,
                        num: 3,
                        checked: false
                    },
                    {
                        id: 3,
                        name: '阿玛尼405',
                        price: 350,
                        num: 2,
                        checked: false
                    }
                ]
            },
            computed: {
                totalPrice(){
                    let total = 0;
                    this.goodsList.forEach(item => {
                        if(item.checked){
                            total += item.price * item.num
                        }
                        
                    });

                    return total
                }
            },
            watch:{
                
                goodsList:{
                    
                    deep: true,
                    handler(){
                        // this.allchecked = this.goodsList.every(item=>item.checked)
                         this.allchecked  = !this.goodsList.some(item => item.checked == false)
                        
                    }
                },
                
            },
            methods: {
                checkall() {
                    
                    this.goodsList.forEach(item => {
                        item.checked = this.allchecked;
                    })
                }
            }
        })
    </script>
</body>

</html>